<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar class="navbar">
      <template #title>
        <van-button
          plain
          round
          icon="search"
          class="searchBtn"
          type="info"
          @click="$router.push('/search')"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>

    <van-tabs v-model="active">
      <van-tab :title="item.name" v-for="item in channelsList" :key="item.id">
        <article-list :channel-id="item.id"></article-list>
      </van-tab>
      <span class="iconfont icon-gengduo" @click="isShowEdit = true"></span>
    </van-tabs>

    <!-- 弹层 -->
    <van-popup
      v-model="isShowEdit"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%', width: '100%' }"
    >
      <edit-popup
        :myChannels="channelsList"
        @updateActive=";[(active = $event), (isShowEdit = false)]"
        @delChannel="delChannel"
        @addChannel="addChannel"
      ></edit-popup>
    </van-popup>
  </div>
</template>

<script>
import { getPindaoAPI, deletePindaoAPI, addPindaoAPI } from '@/api'
import ArticleList from './components/ArticleList.vue'
import EditPopup from './components/EditPopup.vue'
import { mapGetters, mapMutations } from 'vuex'
export default {
  components: { ArticleList, EditPopup },
  created() {
    this.getPindao()
  },
  data() {
    return {
      active: 0,
      channelsList: [],
      isShowEdit: false
    }
  },
  computed: {
    ...mapGetters(['isLogin'])
  },
  methods: {
    ...mapMutations(['SET_CHANNELS']),
    async getPindao() {
      if (!this.isLogin) {
        const { bdChannels } = this.$store.state
        if (bdChannels.length > 0) {
          this.channelsList = bdChannels
        } else {
          const { data } = await getPindaoAPI() // console.log(data);
          this.channelsList = data.data.channels
          this.SET_CHANNELS(this.channelsList)
        }

        // this.channelsList =
      } else {
        const { data } = await getPindaoAPI() // console.log(data);
        this.channelsList = data.data.channels
      }

      // console.log(this.channelsList);
    },
    async delChannel(id) {
      //删除视图层
      try {
        this.$toast.loading({
          message: '正在删除频道...',
          forbidClick: true
        })
        this.channelsList = this.channelsList.filter((item) => item.id !== id)
        if (this.isLogin) {
          await deletePindaoAPI(id)
        } else {
          this.SET_CHANNELS(this.channelsList)
        }
        this.$toast.success('删除成功')
      } catch (error) {
        this.$toast.fail('删除失败')
      }
    },
    async addChannel(item) {
      //添加
      try {
        this.$toast.loading({
          message: '正在添加频道...',
          forbidClick: true
        })
        this.channelsList.push(item)

        if (this.isLogin) {
          await addPindaoAPI(item.id, this.channelsList.length)
        } else {
          this.SET_CHANNELS(this.channelsList)
        }
        this.$toast.success('添加成功')
      } catch (error) {
        this.$toast.fail('添加失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
:deep(.navbar) {
  background-color: #3296fa;
  .van-nav-bar__title {
    max-width: unset;
  }
  .van-icon-search {
    color: #fff;
  }
  .searchBtn {
    width: 7.4rem;
    height: 0.85333rem;
    border: none;
    background-color: #5babfb;
    color: #fff;
  }
}
/* tabs导航条样式 */
:deep(.van-tabs__wrap) {
  padding-right: 66px;

  .van-tabs__nav {
    padding-left: 0;
    padding-right: 0;

    /* tab标签 */
    .van-tab {
      border: 1px solid #eee;
      width: 200px;
      height: 82px;
    }

    /* tab标签下划线 */
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
      bottom: 40px;
    }
  }
}

/* 字体图标 */
.icon-gengduo {
  position: absolute;
  top: 0;
  right: 0;
  width: 66px;
  height: 82px;
  font-size: 40px;
  line-height: 82px;
  text-align: center;
  opacity: 0.6;
  border-bottom: 1px solid #eee;

  &::after {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    height: 70%;
    width: 1px;
    background-image: url('~@/assets/images/gradient-gray-line.png');
  }
}
</style>